package cn.darkhorse.workorder.service.impl;

import java.util.List;

import cn.darkhorse.basic.dto.BalanceRecordDTO;
import cn.darkhorse.workorder.domain.QmsWorkorderBalanceInfo;
import cn.darkhorse.workorder.domain.QmsWorkorderSampleBalanceRecord;
import cn.darkhorse.workorder.service.IQmsWorkorderBalanceInfoService;
import com.alibaba.fastjson2.JSONArray;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.darkhorse.workorder.mapper.QmsWorkorderSampleBalanceMapper;
import cn.darkhorse.workorder.domain.QmsWorkorderSampleBalance;
import cn.darkhorse.workorder.service.IQmsWorkorderSampleBalanceService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 样品平衡Service业务层处理
 *
 * @author ytq
 * @date 2024-07-19
 */
@Service
public class QmsWorkorderSampleBalanceServiceImpl extends ServiceImpl<QmsWorkorderSampleBalanceMapper, QmsWorkorderSampleBalance> implements IQmsWorkorderSampleBalanceService
{

    @Autowired
    private IQmsWorkorderBalanceInfoService workorderBalanceInfoService;

    /**
     * 查询样品平衡
     *
     * @param orderNumber 样品平衡主键
     * @return 样品平衡
     */

    @Override
    public QmsWorkorderSampleBalance getByOrderNumber(String orderNumber) {
        return lambdaQuery().eq(QmsWorkorderSampleBalance::getOrderNumber,orderNumber).one();
    }


    /**
     * 新增样品平衡
     *
     * @param qmsWorkorderSampleBalance 样品平衡
     * @return 结果
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public boolean saveQmsWorkorderSampleBalance(QmsWorkorderSampleBalance qmsWorkorderSampleBalance) {


        QmsWorkorderBalanceInfo balanceInfoBefore = workorderBalanceInfoService.lambdaQuery().eq(QmsWorkorderBalanceInfo::getOrderNumber, qmsWorkorderSampleBalance.getOrderNumber()).eq(QmsWorkorderBalanceInfo::getType, 2).one();
        if (balanceInfoBefore==null){
            balanceInfoBefore = new QmsWorkorderBalanceInfo();
        }
        balanceInfoBefore.setOrderId(qmsWorkorderSampleBalance.getId());
        balanceInfoBefore.setOrderNumber(qmsWorkorderSampleBalance.getOrderNumber());
        balanceInfoBefore.setType(2);
        balanceInfoBefore.setTestRecord(qmsWorkorderSampleBalance.getBeforeTestRecord());
        balanceInfoBefore.setTesterCode(qmsWorkorderSampleBalance.getBeforeTesterCode());
        balanceInfoBefore.setTesterModel(qmsWorkorderSampleBalance.getBeforeTesterModel());
        balanceInfoBefore.setRemark(qmsWorkorderSampleBalance.getBeforeRemark());
        balanceInfoBefore.setTestTime(qmsWorkorderSampleBalance.getBeforeTestTime());
        workorderBalanceInfoService.saveOrUpdate(balanceInfoBefore);

        QmsWorkorderBalanceInfo balanceInfoAfter = workorderBalanceInfoService.lambdaQuery().eq(QmsWorkorderBalanceInfo::getOrderNumber, qmsWorkorderSampleBalance.getOrderNumber()).eq(QmsWorkorderBalanceInfo::getType, 3).one();
        if (balanceInfoAfter==null){
            balanceInfoAfter = new QmsWorkorderBalanceInfo();
        }
        balanceInfoAfter.setOrderId(qmsWorkorderSampleBalance.getId());
        balanceInfoAfter.setOrderNumber(qmsWorkorderSampleBalance.getOrderNumber());
        balanceInfoAfter.setType(3);
        balanceInfoAfter.setTestRecord(qmsWorkorderSampleBalance.getAfterTestRecord());
        balanceInfoAfter.setTesterCode(qmsWorkorderSampleBalance.getAfterTesterCode());
        balanceInfoAfter.setTesterModel(qmsWorkorderSampleBalance.getAfterTesterModel());
        balanceInfoAfter.setRemark(qmsWorkorderSampleBalance.getAfterRemark());
        balanceInfoAfter.setTestTime(qmsWorkorderSampleBalance.getAfterTestTime());
        workorderBalanceInfoService.saveOrUpdate(balanceInfoAfter);
        return saveOrUpdate(qmsWorkorderSampleBalance);
    }



    @Override
    public void saveBalanceRecord(BalanceRecordDTO dto) {
        int type = dto.getType();
        String orderNumber = dto.getOrderNumber();
        JSONArray records = dto.getRecords();
        QmsWorkorderBalanceInfo info = workorderBalanceInfoService.lambdaQuery().eq(QmsWorkorderBalanceInfo::getOrderNumber, orderNumber).eq(QmsWorkorderBalanceInfo::getType, type).one();
        if (info==null){
            info = new QmsWorkorderBalanceInfo();
            info.setOrderNumber(orderNumber);
            info.setType(type);
            info.setOrderId(getByOrderNumber(orderNumber).getId());
        }
        info.setTestRecord(records.toJSONString());
        workorderBalanceInfoService.saveOrUpdate(info);
    }

    @Override
    public List<QmsWorkorderSampleBalanceRecord> getBalanceRecord(BalanceRecordDTO dto) {
        int type = dto.getType();
        String orderNumber = dto.getOrderNumber();
        QmsWorkorderBalanceInfo balanceInfo = workorderBalanceInfoService.lambdaQuery().eq(QmsWorkorderBalanceInfo::getOrderNumber, orderNumber).eq(QmsWorkorderBalanceInfo::getType, type).one();
        return JSONArray.parseArray(balanceInfo.getTestRecord(), QmsWorkorderSampleBalanceRecord.class);

    }
}
